package com.lmh.graphdisease.utils;

import com.huaban.analysis.jieba.JiebaSegmenter;

import java.io.*;
import java.util.*;

/**
 * @author lmh
 * @desc jieba分词的使用(包含去停用词、分词、根据词频筛选)
 * @date 2023/3/8
 */
public class JieBaUtil {

    /**
     *@author lmh
     *@date 2023/3/8
     *@desc 使用jieba分词工具进行分词
     */
    public static List<String> getSentences(String str){
        str = str.replaceAll("\r|\n|\\s", "");
        JiebaSegmenter segmenter = new JiebaSegmenter();
        List<String> resultSentences = segmenter.sentenceProcess(str);
        List<String> stopWords = getFileContent("src/main/java/com/lmh/graphdisease/utils/dict/stopWords.txt");
        resultSentences.removeAll(stopWords);
        return resultSentences;
    }

    /**
     *@author lmh
     *@date 2023/3/8
     *@desc 从txt文本中获取停用词,并返回停用词list
     */
    public static List<String> getFileContent(String path){
        List<String> strList = new ArrayList<String>();
        File file = new File(path);
        InputStreamReader read = null;
        BufferedReader reader = null;
        try {
            read = new InputStreamReader(new FileInputStream(file),"utf-8");
            reader = new BufferedReader(read);
            String line;
            while ((line = reader.readLine()) != null) {
                strList.add(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
        return strList;
    }

    /**
     *@author lmh
     *@date 2023/3/8
     *@desc 根据list中的String出现次数，进行次品排序。便于前端生成词云
     */
    public static Map<String,Integer> getWordsByJieBaUtil(List<String> preLists){
        Map<String,Integer> maps = new HashMap<>();
        for (String s:preLists){
            if(s.contains("。")){
                continue;
            }
            if(maps.containsKey(s)){
                int curNum = maps.get(s);
                maps.put(s,curNum+1);
            }else {
                maps.put(s,1);
            }
        }
        if(preLists.size() <= 50){
            return maps;
        }
        List<Map.Entry<String,Integer>> list = new ArrayList<>(maps.entrySet()); //将map的entryset放入list集合
        //对list进行排序，并通过Comparator传入自定义的排序规则
        Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue()-o1.getValue(); //重写排序规则，小于0表示升序，大于0表示降序
            }
        });
        //用迭代器对list中的键值对元素进行遍历
        Map<String,Integer> result = new HashMap<>();
        Iterator<Map.Entry<String, Integer>> iter = list.iterator();
        int curNum = 0;
        while(iter.hasNext() && curNum < 50){
            Map.Entry<String, Integer> item = iter.next();
            String key = item.getKey();
            int value = item.getValue();
            result.put(key,value);
            curNum++;
//            System.out.println("键:"+key+"   值:"+value);
        }
        return result;
    }


    /**
     *@author lmh
     *@date 2023/3/9
     *@desc 一步到位的方法，直接从String转到map
     */
    public static Map<String,Integer> getMapFromStr(String str){
        List<String> sentences = getSentences(str);
        return getWordsByJieBaUtil(sentences);
    }

}
